import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';

class MySwiper extends StatefulWidget {
  @override
  _MySwiperState createState() => _MySwiperState();
}

class _MySwiperState extends State<MySwiper> {
  int _currentPageIndex = 0;
  var timer;
  PageController _pageController = PageController(initialPage: 1);

  List<String> imgs = [
    "https://img1.qunarzz.com/qs/2101/b5/945dfe4ce22d2302.jpg",
    "https://img1.qunarzz.com/qs/2103/de/7c7e2cdf695f2602.jpg",
    "https://img1.qunarzz.com/qs/2101/87/f3fa48c8346dab02.jpg",
  ];

  @override
  void initState() {
    print("initState");
    // _setTimer();
    super.initState();
  }

  // void _setTimer() {
  //   print("hello");
  //   const timeout = const Duration(seconds: 5);
  //   Timer.periodic(timeout, (timer) {
  //     print('afterTimer=' + DateTime.now().toString());
  //     print(_currentPageIndex);
  //     if (_currentPageIndex == 2) {
  //       setState(() {
  //         _currentPageIndex = 0;
  //       });
  //     } else {
  //       setState(() {
  //         _currentPageIndex += 1;
  //       });
  //     }
  //     _pageController.jumpToPage(_currentPageIndex);
  //     this.timer = timer;
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    return Container(
        height: 170,
        width: double.infinity,
        margin: EdgeInsets.only(bottom: 10, left: 10, right: 10, top: 10),
        decoration: ShapeDecoration(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(20.0)),
            side: BorderSide(
              width: 0,
              color: Color.fromARGB(250, 211, 211, 211),
              style: BorderStyle.solid,
            ),
          ),
        ),
        child: Stack(
          children: [
            NotificationListener(
              onNotification: (ScrollNotification notification) {
                if (notification.depth == 0 &&
                    notification is ScrollStartNotification) {
                  if (notification.dragDetails != null) {
                    print(notification.dragDetails);
                  }
                } else if (notification is ScrollEndNotification) {
                  print("hello");
                }
              },
              child: PageView.builder(
                  onPageChanged: (int index) {
                    setState(() {
                      _currentPageIndex = index % (imgs.length);
                    });
                  },
                  itemCount: 10000,
                  itemBuilder: (context, index) {
                    return ClipRRect(
                      borderRadius: BorderRadius.circular(7),
                      child: Image.network(
                        imgs[index % (imgs.length)],
                        fit: BoxFit.cover,
                      ),
                    );
                  }),
            ),
            Positioned(
              bottom: 10,
              left: 0,
              right: 0,
              child: Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(imgs.length, (i) {
                    return Container(
                      margin: EdgeInsets.symmetric(horizontal: 2),
                      width: 5,
                      height: 5,
                      decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: _currentPageIndex == i
                              ? Colors.blue
                              : Colors.grey),
                      child: InkWell(
                        onTap: () {
                          setState(() {
                            _currentPageIndex = i;
                          });
                          _pageController.jumpToPage(_currentPageIndex);
                        },
                      ),
                    );
                  }).toList(),
                ),
              ),
            ),
//            Positioned(
//                top: 90,
//                child: InkWell(
//                    onTap: () {
//                      if(_currentPageIndex==0){
//                        setState(() {
//                          _currentPageIndex=2;
//                        });
//                      }else{
//                        _currentPageIndex-=1;
//                      }
//                      _pageController.jumpToPage(_currentPageIndex);
//                    },
//                    child: Container(
//                      width: 50,
//                      height: 50,
//                      alignment: Alignment.center,
//                      child: Icon(
//                        IconData(0xe652, fontFamily: "alIcons"),
//                        size: 25,
//                      ),
//                    ))),
//            Positioned(
//                top: 90,
//                right: 2,
//                child: InkWell(
//                    onTap: () {
//                        if(_currentPageIndex==2){
//                          setState(() {
//                            _currentPageIndex=0;
//                          });
//                        }else{
//                          setState(() {
//                            _currentPageIndex+=1;
//                          });
//                        }
//                        _pageController.jumpToPage(_currentPageIndex);
//                    },
//                    child: Container(
//                      width: 50,
//                      height: 50,
//                      alignment: Alignment.center,
//                      child: Icon(
//                        IconData(0xe60e, fontFamily: "alIcons"),
//                        size: 25,
//                      ),
//                    ))),
          ],
        ));
  }

  @override
  void dispose() {
    // timer.cancel();
    super.dispose();
  }
}
